#   Level class
#   Used to read bve config files
#   
#   They have the formant of:
#
#   [block]
#   0,0,0
#   1,1,1
#   [nest]
#   2,2,2
#   3,3,3
#


class Level():
    rawleveldata = ""
    blocks = []
    cannon = None

    def __init__(self, sfile):
        try:
            f = open(sfile, "r")
            self.rawleveldata = f.read()
            self.parseLevel()
        except Exception, e:
            raise Exception("There was an error loading the level file: " + str(e))

    def parseLevel(self):
        blockdata = self.getBlockSection()
        for block in blockdata.split("\n"):
            blockarr = block.split(",")
            self.blocks.append((blockarr[0],blockarr[1]))
        nestdata = self.getNestSection()
        print blockdata
      
    def getBlockSection(self):
        return self.getSection("block")

    def getNestSection(self):
        return self.getSection("nest")

    def getCannonSection(self):
        return self.getSection("cannon")

    def getSection(self, section_name):
        begindata = self.rawleveldata.find("[" + section_name + "]") + len(section_name) + 2;
        if begindata == -1:
            return ""
        enddata = self.rawleveldata.find("[", begindata)
        if enddata == 0 or enddata == -1:
            enddata = len(self.rawleveldata)
        return self.rawleveldata[begindata:enddata].strip()
